﻿using System;
using ALApps.Graphics.Interface.Engine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SunrayEngine.MapEditor.EditTools
{
    #region delegates
    public delegate void TextureChangedEventHandler(object sender, TextureChangedEventArgs e);
    public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);
    #endregion

    public sealed class Brush
    {
        #region fields
        private Texture2D selectedTexture;
        private Color color;
        #endregion

        #region properties
        public bool Active { get; set; }

        public int Alpha { get; set; }

        public int Size { get; set; }

        public Texture2D PaintTexture
        {
            get { return selectedTexture; }
            set
            {
                TextureChanged(this, new TextureChangedEventArgs(selectedTexture, value));
                selectedTexture = value;
            }
        }

        public Texture2D Texture { get; set; }

        public Color Color
        {
            get { return color; }
            set
            {
                ColorChanged(this, new ColorChangedEventArgs(color, value));
                color = value;
            }
        }
        #endregion

        #region initialization
        public Brush()
        {
            Alpha = 100;
            selectedTexture = InterfaceEngine.Scripting.Execute("Textures['prim']");
            Texture = selectedTexture;
            color = Color.White;

            InterfaceEngine.Scripting["setBrushTexture"] = new Action<Texture2D>(setTexture);
            InterfaceEngine.Scripting["setBrushColor"] = new Action<Color>(setColor);

            TextureChanged = new TextureChangedEventHandler((s, e) => { });
            ColorChanged = new ColorChangedEventHandler((s, e) => { });
        }
        #endregion

        #region methods
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                Vector2 mPos = InterfaceEngine.Input.MousePosition;
                spriteBatch.Draw(Texture, new Rectangle((int)mPos.X - Size, (int)mPos.Y - Size, Size, Size), Color * .5f);
            }
        }

        public Rectangle GetBounds()
        {
            Vector2 mPos = InterfaceEngine.Input.MousePosition;
            return new Rectangle((int)mPos.X - Size, (int)mPos.Y - Size, Size, Size);
        }

        private void setTexture(Texture2D texture)
        {
            PaintTexture = texture;
        }

        private void setColor(Color color)
        {
            Color = color;
        }
        #endregion

        #region events
        public event TextureChangedEventHandler TextureChanged;
        public event ColorChangedEventHandler ColorChanged;
        #endregion
    }

    #region event args
    public class TextureChangedEventArgs : EventArgs
    {
        #region properties
        public Texture2D OldTexture { get; protected set; }
        public Texture2D NewTexture { get; protected set; }
        #endregion

        public TextureChangedEventArgs(Texture2D oldTexture, Texture2D newTexture)
        {
            OldTexture = oldTexture;
            NewTexture = newTexture;
        }
    }

    public class ColorChangedEventArgs : EventArgs
    {
        #region properties
        public Color OldColor { get; protected set; }
        public Color NewColor { get; protected set; }
        #endregion

        public ColorChangedEventArgs(Color oldColor, Color newColor)
        {
            OldColor = oldColor;
            NewColor = newColor;
        }
    }
    #endregion
}
